package com.qf.ssm.provider;


import com.alibaba.druid.util.StringUtils;
import com.qf.ssm.dto.ResultCode;
import com.qf.ssm.exceptions.UpdateErrorException;
import com.qf.ssm.pojo.Categories;

public class CategorySqlProvider {

    public String getUpdateSql(Categories category) {
        boolean isNeedUpdate=false;//是否需要更新,主要是万一什么参数都没传递, sql 语句会出现语法问题
        StringBuffer stringBuffer = new StringBuffer("UPDATE categories SET");
        if (!StringUtils.isEmpty(category.getCategoryname())) {
            stringBuffer.append(" categoryname = #{categoryname},");
            isNeedUpdate = true;
        }
        if (!StringUtils.isEmpty(category.getTxcommand())) {
            stringBuffer.append(" txcommand = #{txcommand},");
            isNeedUpdate = true;
        }
        if (!StringUtils.isEmpty(category.getRxcommand())) {
            stringBuffer.append(" rxcommand = #{rxcommand},");
            isNeedUpdate = true;
        }
        if (category.getStatus()!=null&&!category.getStatus().equalsIgnoreCase("-100")) {
            stringBuffer.append(" status = #{status},");
            isNeedUpdate = true;
        }
        if (!isNeedUpdate) {
            throw new UpdateErrorException("没有传递要更新的内容,请确认", ResultCode.DATA_NOT_ALLOW_NUL);
        }
        stringBuffer.deleteCharAt(stringBuffer.lastIndexOf(","));
        stringBuffer.append(" WHERE id = #{id}");
        return stringBuffer.toString();
    }
}
